package com.link510.aitools.activity.airobot.audio;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.link510.aitools.R;
import com.link510.aitools.core.listen.CWMListener;
import com.link510.aitools.core.message.MessageInfo;
import com.link510.aitools.framework.activity.BaseAcitivity;
import com.link510.aitools.framework.adapter.AudioShoutingAdapter;
import com.link510.aitools.framework.view.audio.AudioRecordManager;
import com.link510.aitools.framework.view.audio.widget.RecordAudioButton;
import com.link510.aitools.services.Airobots;
import com.link510.aitools.services.QiniuUtils;
import com.link510.aitools.services.CustomWaitDialogUtils;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import omrecorder.PullTransport;

/**
 * 远程喊话
 */
public class RobotAudioShoutingActivity extends BaseAcitivity {

    @BindView(R.id.rootLayout)
    LinearLayout mRoot;

    @BindView(R.id.btnVoice)
    RecordAudioButton mBtnVoice;//底部录制按钮;

    @BindView(R.id.rvMsgList)
    ListView rvMsgList;

    private CustomWaitDialogUtils customWaitDialogUtils;

    private AudioRecordManager audioRecordManager;

    /**
     * 文件列表
     */
    private List<File> audioFileList = Lists.newArrayList();

    private AudioShoutingAdapter mAdapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_robot_audio_shouting);

        initUI();
    }


    private void initUI() {
        customWaitDialogUtils = new CustomWaitDialogUtils(this.mContext);
        audioRecordManager = new AudioRecordManager(this.mContext) {
            /**
             * 文件上传
             * @param audioFile 上传
             */
            @Override
            public void toUploadAction(File audioFile) {

                if (audioFileList == null) {
                    audioFileList = Lists.newArrayList();
                }
                audioFileList.add(audioFile);
                mAdapter.notifyDataSetChanged();
                RobotAudioShoutingActivity.this.toUploadAction(audioFile);
            }

            /**
             * 监听
             *
             * @return
             */
            @Override
            public PullTransport.OnAudioChunkPulledListener audioChunkPulledListener() {
                return audioChunk -> {
                    updateCurrentVolume(audioChunk.maxAmplitude());
                };
            }

            @Override
            public View getAnchor() {
                return mRoot;
            }

        };

        mBtnVoice.setOnVoiceButtonCallBack(new RecordAudioButton.OnVoiceButtonCallBack() {
            @Override
            public void onStartRecord() {
                audioRecordManager.startRecording();
            }

            @Override
            public void onStopRecord() {
                audioRecordManager.stopRecording();
            }
        });

        mAdapter = new AudioShoutingAdapter(this.mContext) {

            @Override
            public List<File> getFileList() {

                if (RobotAudioShoutingActivity.this.audioFileList == null) {
                    RobotAudioShoutingActivity.this.audioFileList = Lists.newArrayList();
                }
                return RobotAudioShoutingActivity.this.audioFileList;
            }

            /**
             * 文件处理
             *
             * @param file file
             */
            @Override
            public void toLongClickAction(File file) {
                if (FileUtils.delete(file)) {
                    RobotAudioShoutingActivity.this.audioFileList.remove(file);
                    RobotAudioShoutingActivity.this.mAdapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showShort("删除失败");
                }
            }

        };//适配器


        rvMsgList.setAdapter(mAdapter);

    }


    /**
     * 文件上传
     *
     * @param file 上传
     */

    public void toUploadAction(File file) {

        if (file == null) {
            return;
        }

        if (!file.exists()) {
            customWaitDialogUtils.close();
            ToastUtils.showShort("文件转换失败");
            return;
        }

        customWaitDialogUtils.show("文件上传中...");

        QiniuUtils.getInstance().upload(file, new CWMListener<String>() {
            @Override
            public void onSuccess(String message, String mp3Url) {

                if (Strings.isNullOrEmpty(mp3Url)) {
                    customWaitDialogUtils.close();
                    ToastUtils.showShort("文件上传失败");
                    return;
                }

                customWaitDialogUtils.show("文件上传中成功...");

                Logger.e("文件上传成功:" + JSON.toJSONString(mp3Url));

                customWaitDialogUtils.show("命令发送中...");
                Airobots.getInstance().audioShoutingUrl(RobotAudioShoutingActivity.this.deviceSN, mp3Url, new CWMListener<MessageInfo<String>>() {

                    /**
                     * 解析成功
                     * <p>
                     * * @param message 消息
                     *
                     * @param message
                     * @param messageInfo 实体消息
                     */
                    @Override
                    public void onSuccess(String message, MessageInfo<String> messageInfo) {
                        customWaitDialogUtils.close();
                        ToastUtils.showShort("命令下发成功");
                    }

                    /**
                     * 返回错误解析
                     *
                     * @param code    错误状态码
                     *                * @param message 消息
                     * @param message
                     */
                    @Override
                    public void onError(int code, String message) {

                        customWaitDialogUtils.close();
                        ToastUtils.showShort("命令下发成功");
                    }
                });

            }

            @Override
            public void onError(int code, String message) {
                customWaitDialogUtils.close();
                ToastUtils.showShort(message);
            }
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (customWaitDialogUtils != null) {
            customWaitDialogUtils.dismiss();
        }
    }

}